
const USER = 'hui_zhuang_user_sessionStorage'

const createStorage = () => {
  sessionStorage.setItem(USER, encodeURIComponent('{}'))
}

export function setStorage (key, value) {
  if (!sessionStorage.getItem(USER)) {
    createStorage()
  }
  const user = JSON.parse(decodeURIComponent(sessionStorage.getItem(USER)))
  user[key] = value
  sessionStorage.setItem(USER, encodeURIComponent(JSON.stringify(user)))
}

export function getStorage (key) {
  if (!sessionStorage.getItem(USER)) {
    createStorage()
  }
  const session = JSON.parse(decodeURIComponent(sessionStorage.getItem(USER)))
  return session[key]
}

export function removeStorage () {
  sessionStorage.removeItem(USER)
}
